// 主界面
;define(function(require, exports, module) {
    'use strict';
    var util = require("util");
    var base = require(CONFIG.BasePath + "js/enterprise/base");
    var router = require("../hash/pageRouter");
    var constant = require("constant");
    var css_letter = require("./accountLetter.css");
    var databind = require("databind");
    var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
    
    var init = function(){
    	router.init();
    	inquireLetterListReceived();
    	
		initEvent();
    };
    
	var initEvent = function(){
		if(base.getData("sid")==null){
			location.href="../login/login.html";
		}
		
		$(".financeDynamic").on("click",function(){
			$(".dynamic").removeClass("none");
			$(".announce").addClass("none");
			$(".systemAnnounce").parent().removeClass("span1");
			$(".systemAnnounce").parent().addClass("span2");
			$(".financeDynamic").parent().addClass("span1");
			$(".financeDynamic").parent().removeClass("span2");
			
			pageIndex = 1;
			pageSize = 10;
			totalPageCount = 1;
			totalRecordCount = 0; //分页初始化
			inquireLetterListReceived();
		});
		
		$(".systemAnnounce").on("click",function(){
			$(".announce").removeClass("none");
			$(".dynamic").addClass("none");
			$(".systemAnnounce").parent().removeClass("span2");
			$(".systemAnnounce").parent().addClass("span1");
			$(".financeDynamic").parent().addClass("span2");
			$(".financeDynamic").parent().removeClass("span1");
			pageIndex = 1;
			pageSize = 10;
			totalPageCount = 1;
			totalRecordCount = 0; //分页初始化
			inquireEnterpriseBulletinList();
		});
    };
    
    //系统公告
    var inquireEnterpriseBulletinList = function(){
		base.fnAjax({
			action: "inquireEnterpriseBulletinList.json",
			oData: {
                pageSize: pageSize,
           		pageIndex: pageIndex
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                	var tpl = $("#announceTpl").html();
                	var html = juicer(tpl, data);
                	$("#announceBody").html(html);
                	$(".announce_title").unbind("click").click(function(){
                	//http://localhost:1111/finance/html/enterprise/main/bullitinDetail.html?bulletinId=155
                	var bulletinId = $(this).parent().parent().attr("bulletinId");
                	location.href="../main/bullitinDetail.html?bulletinId="+bulletinId;
                	});
                	totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
                	base.pageControl($("#accountLetterPage"),
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							inquireEnterpriseBulletinList();
						});
                } else {
                }
            }
		});
	};
    
	//企业查询融资信息列表
    var inquireLetterListReceived = function(){
		base.fnAjax({
			action: "inquireLetterListReceived.json",
			async: false,
			oData: {
                pageSize: pageSize,
           		pageIndex: pageIndex
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                	juicer.register("getValueByKey", constant.getValueByKey);
                	var tpl = $("#accountLetterTpl").html();
                	var html = juicer(tpl, data);
                	$("#accountLetterBody").html(html);
                	$(".letterItem").unbind("click").click(function(){
                		markLetterReadReceived($(this).attr("letterId"));
                		$(this).find(".readStatus").html("【已读】");
                	});
                	totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
                	base.pageControl($("#accountLetterPage"),
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							inquireLetterListReceived();
						});
                } else {
                }
            }
		});
	};
	
	//站内信标记为已读
    var markLetterReadReceived = function(letterId){
		base.fnAjax({
			action: "markLetterReadReceived.json",
			async: false,
			oData: {
                letterId: letterId
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                } else {
                }
            }
		});
	};
	
    return {
        init:init
    };
});